package com.teach.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**<pre>
 * Spring上下文工具类
 * Class: com.teach.util.SpringUtil
 * Copyright 2022 jlzhou.top Inc. All rights reserved.
 * Warning: this content is only for internal circulation of the company.
 *          It is forbidden to divulge it or use it for other commercial purposes.
 * </pre>
 * @author J.L.Zhou
 * @since 2022年10月22日 下午1:54:03
 * @version 1.0
 */
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext context;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        SpringUtil.context = applicationContext;
    }

    public static String getActiveProfile() {
        return context.getEnvironment().getActiveProfiles()[0];
    }

    public static <T> T getBean(Class<T> clazz) {
        return context.getBean(clazz);
    }

    public static ApplicationContext getApplicationContext(){
        return context;
    }

    public static JsonUtil getJsonUtil(){
        return getBean(JsonUtil.class);
    }
}
